package convertdate01;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/*
   JDK1.8 对日期对象和日期字符串相互转换
   1.将日期对象转换为日期字符串
     DateTimeFormatter类
       static DateTimeFormatter ofPattern(String pattern)
       String format(TemporalAccessor temporal)
   2.将日期字符串转换为日期对象
      LocalDate  LocalTime  LocalDateTime中都含有一下两个方法

      static LocalDateTime parse(CharSequence text).//将一个日期字符串按一个默认格式解析为日期对象
      static LocalDateTime parse(CharSequence text, DateTimeFormatter formatter)  //可以按照指定格式解析以恶搞日期字符串

 */
public class ConvertDateDemo02 {
    public static void main(String[] args) {
        //method01();

        //method02();

        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        LocalDateTime parse = LocalDateTime.parse("2013/12/13 12:10:11", df);
        System.out.println(parse.getYear());
        System.out.println(parse.getHour());

    }

    private static void method02() {
        LocalDate now = LocalDate.now();
        System.out.println(now);//2023-11-02

        LocalDate parse = LocalDate.parse("2012-10-12");
        System.out.println(parse.getYear());

       /* LocalDate parse2 = LocalDate.parse("2013/11/11");//Text '2013/11/11' could not be parsed
        System.out.println(parse2);*/

        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        LocalDate parse2 = LocalDate.parse("2012/12/13", df);
        System.out.println(parse2.getDayOfMonth());
    }

    private static void method01() {
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);//2023-11-02T22:54:38.160458100

        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println(df.format(now));
    }
}
